#include "netfd_loop_queue.h"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <time.h>

LoopQueue_t *createLoopQueue(int capacity)
{
    LoopQueue_t *queue = (LoopQueue_t *)calloc(1,sizeof(LoopQueue_t));
    if (queue == NULL)
    {
        return NULL;
    }

    queue->array = (HashMap**)calloc(capacity,sizeof(HashMap*));
    queue->size = 0;
    queue->capacity = capacity;
    queue->front = 0;
    queue->rear = 0;


    if (queue->array == NULL)
    {
        free(queue);
        return NULL;
    }

    for (int i = 0; i < capacity; i++)
    {
        queue->array[i] = hashmap_create(); // 每个array[i]都是一个指针，初始化为NULL
    }

    return queue;
}

void destroyLoopQueue(LoopQueue_t *queue)
{
    if (queue == NULL)
    {
        return;
    }
    for (int i = 0; i < queue->capacity; i++){
        if (queue->array[i] != NULL){
            hashmap_destroy(queue->array[i]);
        }
    }
    free(queue->array);
    free(queue);
    printf("free succeed\n");
}

HashMap* enqueueLoop(LoopQueue_t *queue, HashMap* element)//队尾入队
{
    if (queue == NULL || isFull(queue))
    {
        return NULL;
    }
    queue->array[queue->rear] = element;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->size++;
    return element;
}

HashMap* dequeueLoop(LoopQueue_t *queue)//队首出队
{
    if (queue == NULL || isEmpty(queue))
    {
        return NULL;
    }
    HashMap* element = queue->array[queue->front];
    queue->array[queue->front] = NULL;
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size--;

    return element;
}

HashMap* front(LoopQueue_t *queue)
{
    if (queue == NULL || isEmpty(queue))
    {
        return NULL;
    }

    return queue->array[queue->front];
}

HashMap* rear(LoopQueue_t *queue)
{
    if (queue == NULL || isEmpty(queue))
    {
        return NULL;
    }

    return queue->array[(queue->rear - 1 + queue->capacity) % queue->capacity];
}

int isEmpty(LoopQueue_t *queue)
{
    if (queue == NULL)
    {
        return 1;
    }

    return queue->size == 0;
}

int isFull(LoopQueue_t *queue)
{
    if (queue == NULL)
    {
        return 0;
    }

    return queue->size == queue->capacity;
}

// 打印循环队列的函数
/* void printLoopQueue(LoopQueue_t *queue) { */
/*     if (queue == NULL || isEmpty(queue)) { */
/*         printf("队列为空，没有元素可打印。\n"); */
/*         return; */
/*     } */

/*     printf("循环队列中的元素为：\n"); */
/*     for (int i = 0; i < queue->capacity; i++) { */
/*         if(queue->array[i]==NULL){ */
/*             continue; */
/*         } */
/*         printf("%d\n",i); */
/*         for(int j=0;j<10;++j){ */
/*             printf("%d ", (queue->array[i][j])); */
/*         } */
/*         printf("\n"); */
/*     } */
/* } */


//int main(){
//    
//    LoopQueue_t* queue = createLoopQueue(30);
//    HashMap* map = hashmap_create();
//    hashmap_put(map,"4",4);
//    hashmap_put(map,"5",5);
//    hashmap_put(map,"6",6);
//    hashmap_put(map,"7",7);
//    hashmap_put(map,"8",8);
//    
//
//    enqueueLoop(queue,map);
//    
//    HashMap* result;
//
//    result = dequeueLoop(queue);
//    print_hashmap(result);
//    hashmap_remove_all(result);
//    print_hashmap(result);
//
//
//}
